package www.yingdi365.com.viewholder;

import android.support.annotation.LayoutRes;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;
import www.yingdi365.com.R;
import www.yingdi365.com.activity.WoDeQJActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.customview.TwoBtnDialog;
import www.yingdi365.com.model.AppInvitationPaging;
import www.yingdi365.com.model.NormalBack;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.util.LogUtil;

/**
 * Created by Administrator on 2017/3/28 0028.
 */
public class WoDeQJViewHolder extends BaseViewHolder<AppInvitationPaging.InvitationsBean> {

    private final ImageView imageThumb;
    private final View viewShanChu;
    private final ImageView imageShanChu;
    private AppInvitationPaging.InvitationsBean data;

    public WoDeQJViewHolder(ViewGroup parent, @LayoutRes int res) {
        super(parent, res);
        imageThumb = $(R.id.imageThumb);
        viewShanChu = $(R.id.viewShanChu);
        imageShanChu = $(R.id.imageShanChu);
        imageShanChu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final TwoBtnDialog twoBtnDialog = new TwoBtnDialog(getContext(), "提示", "（确定要删除该请柬吗？）", "确定", "取消");
                twoBtnDialog.setClicklistener(new TwoBtnDialog.ClickListenerInterface() {
                    @Override
                    public void doConfirm() {
                        twoBtnDialog.dismiss();
                        ((WoDeQJActivity) getContext()).showLoadingDialog();
                        OkObject okObject = getOkObject();
                        OkGo.get(okObject.getUrl())//
                                .tag(this)//
                                .headers(okObject.getHeaders())
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(String s, Call call, Response response) {
                                        ((WoDeQJActivity) getContext()).cancelLoadingDialog();
                                        LogUtil.LogShitou("WoDeQJViewHolder--onSuccess删除我的请柬", s);
                                        try {
                                            NormalBack normalBack = GsonUtils.parseJSON(s, NormalBack.class);
                                            if (normalBack.isSuccess()) {
                                                Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                                                ((WoDeQJActivity) getContext()).onRefresh();
                                            } else {
                                                Toast.makeText(getContext(), "删除失败", Toast.LENGTH_SHORT).show();
                                            }
                                        } catch (Exception e) {
                                            Toast.makeText(getContext(), getContext().getResources().getString(R.string.shujuchucuo), Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                    @Override
                                    public void onError(Call call, Response response, Exception e) {
                                        super.onError(call, response, e);
                                        ((WoDeQJActivity) getContext()).cancelLoadingDialog();
                                        if (response != null) {
                                            if (response.code() == 401) {
                                                ((WoDeQJActivity) getContext()).showReLoginDialog();
                                            }
                                        }
                                        Toast.makeText(getContext(), getContext().getResources().getString(R.string.qingqiushibai), Toast.LENGTH_SHORT).show();
                                    }
                                });
                    }

                    @Override
                    public void doCancel() {
                        twoBtnDialog.dismiss();
                    }
                });
                twoBtnDialog.show();
            }
        });
    }

    private OkObject getOkObject() {
        String url = Constant.HOST + Constant.Url.AppInvitationService + "invitation/remove/" + data.getId();
        HttpHeaders headers = new HttpHeaders();
        headers.put(Constant.TOKEN, ((WoDeQJActivity) getContext()).appLoginResult.getToken());
        HashMap<String, String> params = new HashMap<>();
        return new OkObject(headers, params, url);
    }

    @Override
    public void setData(AppInvitationPaging.InvitationsBean data) {
        super.setData(data);
        this.data = data;
        if (data.isGuanLi()) {
            viewShanChu.setVisibility(View.VISIBLE);
        } else {
            viewShanChu.setVisibility(View.INVISIBLE);
        }
        Glide.with(getContext())
                .load(data.getTmpl().getThumb())
                .placeholder(R.mipmap.preview)
                .into(imageThumb);
    }

}
